﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Rabbit.Controls;
using System.Windows.Documents;

namespace MicroBlogClient.Behaviors
{
    public partial class CommandHelper
    {
        #region Mouse Enter
        public static readonly DependencyProperty MouseEnterProperty =
            DependencyProperty.RegisterAttached("MouseEnter",
                typeof(object),
                typeof(CommandHelper),
                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, MouseEnterPropertyChangedCallBack)
                );
        public static readonly DependencyProperty MouseEnterParameterProperty =
           DependencyProperty.RegisterAttached("MouseEnterParameter",
            typeof(object),
               typeof(CommandHelper));
        public static void SetMouseEnter(FrameworkElement element, object value)
        {
            element.SetValue(MouseEnterProperty, value);
        }

        public static void SetMouseEnterParameter(FrameworkElement element, object value)
        {
            element.SetValue(MouseEnterParameterProperty, value);
        }
        private static void MouseEnterPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            FrameworkElement element = d as FrameworkElement;
            element.MouseEnter += (sender, e) =>
            {
                object param = element.GetValue(MouseEnterParameterProperty);
                //to be refined
                element.Tag = param;
                (element.GetValue(MouseEnterProperty) as ICommand).Execute(element);
            };
        }
        #endregion

        #region Mouse Leave
        public static readonly DependencyProperty MouseLeaveProperty =
           DependencyProperty.RegisterAttached("MouseLeave",
               typeof(object),
               typeof(CommandHelper),
               new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, MouseLeavePropertyChangedCallBack)
               );

        public static readonly DependencyProperty MouseLeaveParameterProperty =
           DependencyProperty.RegisterAttached("MouseLeaveParameter",
            typeof(object),
               typeof(CommandHelper));

        public static void SetMouseLeave(FrameworkElement element, object value)
        {
            element.SetValue(MouseLeaveProperty, value);
        }

        public static void SetMouseLeaveParameter(FrameworkElement element, object value)
        {
            element.SetValue(MouseLeaveParameterProperty, value);
        }

        private static void MouseLeavePropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            FrameworkElement element = d as FrameworkElement;
            element.MouseLeave += (sender, e) =>
            {
                object param = element.GetValue(MouseLeaveParameterProperty);
                (element.GetValue(MouseLeaveProperty) as ICommand).Execute(param);
            };
        } 
        #endregion

        #region On UserName CLicked 
        public static readonly DependencyProperty OnUserNameClickedProperty =
            DependencyProperty.RegisterAttached("OnUserNameClicked",
                                        typeof(object), 
                                        typeof(CommandHelper), 
                                        new PropertyMetadata(
                                                            null,
                                                            (sender, args) => {
                                                                TextBlockEx tb = sender as TextBlockEx;
                                                                tb.OnUserNameClicked += (d, e) => {
                                                                    Hyperlink link = d as Hyperlink;
                                                                    if (link == null)
                                                                    {
                                                                        link = e.OriginalSource as Hyperlink;
                                                                    }
                                                                    (tb.GetValue(OnUserNameClickedProperty) as ICommand).Execute(link.CommandParameter);
                                                                };
                                                                }
                                                            )
                                     );
         
        public static void SetOnUserNameClicked(TextBlockEx textBlock, object value)
        {
            textBlock.SetValue(OnUserNameClickedProperty, value);
        }
        #endregion

#region PreviewMouseLeftButtonDown

        public static DependencyProperty PreviewMouseLeftButtonDownProperty =
            DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown",
            typeof(object), typeof(CommandHelper),
            new PropertyMetadata((sender, args) =>
                {
                    (sender as FrameworkElement).PreviewMouseLeftButtonDown += (d, e) =>
                        {
                            (sender.GetValue(PreviewMouseLeftButtonDownProperty) as ICommand).Execute(sender.GetValue(PreviewMouseLeftButtonDownParameterProperty));
                        };
                }
                ));

        public static void SetPreviewMouseLeftButtonDown(FrameworkElement element, object value)
        {
            element.SetValue(PreviewMouseLeftButtonDownProperty, value);
        }

        public static DependencyProperty PreviewMouseLeftButtonDownParameterProperty =
            DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDownParameter",
            typeof(object), typeof(CommandHelper));
        public static void SetPreviewMouseLeftButtonDownParameter(FrameworkElement element, object value)
        {
            element.SetValue(PreviewMouseLeftButtonDownParameterProperty, value);
        }
	#endregion
        
    }
}
